package com.fanrenqiji.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年5月8日 上午10:11:24
 * @description
 */
public class StringDateUtil extends DateUtils {
	private final static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	private final static String YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
	private final static String yyyyMMdd = "yyyyMMdd";
	private final static String YYYY_MM_DD = "yyyy-MM-dd";

	/**
	 * 将当前日期转化成yyyyMMddHHmmss字符串
	 * 
	 * @return
	 */
	public static String getCurrentyyyyMMdd() {
		SimpleDateFormat fmt = new SimpleDateFormat(yyyyMMdd);
		return fmt.format(new Date());
	}

	/**
	 * 将日期转化成yyyy-MM-dd字符串
	 * 
	 * @return
	 */
	public static String getDateYYYY_MM_DD(Date date) {
		SimpleDateFormat fmt = new SimpleDateFormat(YYYY_MM_DD);
		if (date == null)
			return null;
		return fmt.format(date);
	}

	/**
	 * 将当前日期转化成yyyyMMddHHmmss字符串
	 * 
	 * @return
	 */
	public static String getCurrentyyyyMMddHHmmss() {
		SimpleDateFormat fmt = new SimpleDateFormat(yyyyMMddHHmmss);
		return fmt.format(new Date());
	}

	/**
	 * 将当前日期转化成yyyy-MM-dd HH:mm:ss字符串 24小时制
	 * 
	 * @return
	 */
	public static String getCurrentDateAndTime() {
		SimpleDateFormat fmt = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
		return fmt.format(new Date());
	}

	/**
	 * 返回的时间如:2014-04-02 15:31:23.66
	 *
	 * @return
	 */
	public static Date stringParse2Date(String strDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
		if (strDate.length() == 10) {
			strDate = strDate.concat(" 00:00:00");
		}
		return sdf.parse(strDate);
	}

	/**
	 * @throws ParseException
	 * 
	 */
	public static Date getWorkDate(Date date) {
		if (date == null) {
			date = new Date();
		}
		try {
			Date startWork = stringParse2Date(getDateYYYY_MM_DD(date)
					+ " 09:00:00");
			Date endWork = stringParse2Date(getDateYYYY_MM_DD(date)
					+ " 21:00:00");
			if (date.getTime() < startWork.getTime()) {
				return startWork;
			} else if (date.getTime() > endWork.getTime()) {
				return stringParse2Date(getDateYYYY_MM_DD(addDays(date, 1))
						+ " 09:00:00");
			} else {
				return date;
			}
		} catch (Exception e) {
		}
		return date;
	}

	public static void main(String[] args) {
		try {
			System.out.println(getCurrentyyyyMMdd());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
